<?php
/**
 * 专营店反馈项目信息表
 * @author qianxun
 *
 */
class storeProModel extends MY_Model{
    function storeProModel(){
        parent::MY_Model();
        $this->table = 'StorePro';
    }
    /**
     * 包装处理数据(non-PHPdoc)
     * @see MY_Model::packing()
     */
    function packing($list = array()) {
        //时段配置列表
        $this->load->model('timeUnitModel');
        $timeUnitList = $this->timeUnitModel->getCacheData('timeUnitList');
        
        $configAuditRs = $this->config->item('auditRs');
        foreach ($list as $k => $v){
            $v['itemValue'] = unserialize($v['itemValue']);
            $v['auditValue'] = unserialize($v['auditValue']);
            $v['auditRsStr'] = isset($configAuditRs[$v['auditRs']]) ? $configAuditRs[$v['auditRs']] : '';
            //时段名称
            $v['timeUnitStr'] = isset($timeUnitList[$v['year']][$v['timeUnit']]) ? $timeUnitList[$v['year']][$v['timeUnit']] : '';
            $list[$k] = $v;
        }
        return $list;
    }
    /**
     * 获取专营店项目数据列表，并以项目id为键
     * @param number $storeId
     */
    function getProListKeyProId($storeId){
        $findList = $this->getNewList(array('storeId'=>$storeId));
        $list = array();
        foreach ($findList as $v){
            $list[$v['proId']] = $v;
        }
        return $list;
    }
    /**
     * 获取专营店项目列表
     * @param number $id
     */
    function getActList($id) {
        $proActAuditItems = $this->config->item('proActAuditItems');
        $storeProData = $this->getNewData($id);
        $this->load->model('proActModel');
        $actList = $this->proActModel->getActListByProId($storeProData['proId']);
        foreach ($actList as $actK => $actV){
            //项目审核结果合并
            $auditValue = isset($storeProData['auditValue'][$actV['actId']]) ? $storeProData['auditValue'][$actV['actId']] : array('auditRs'=>$storeProData['auditRs'],'auditDes'=>'');
            $auditValue['auditRsStr'] = isset($proActAuditItems[$auditValue['auditRs']]) ? $proActAuditItems[$auditValue['auditRs']] : '';
            if ($auditValue['auditDes']){
                $auditValue['auditRsStr'] .= '('.$auditValue['auditDes'].')';
            }
            $actV['auditValue'] = $auditValue;
            //题目答题值合并
            foreach ($actV['itemList'] as $itemK => $itemV){
                if (in_array($itemV['type'], array(2,3,6,7))){
                    $itemV['value'] = isset($storeProData['itemValue'][$itemV['itemId']]) ? (array)$storeProData['itemValue'][$itemV['itemId']] : array();                    
                }else{
                    $itemV['value'] = isset($storeProData['itemValue'][$itemV['itemId']]) ? $storeProData['itemValue'][$itemV['itemId']] : '';
                }
                $actV['itemList'][$itemK] = $itemV;
            }
            $actList[$actK] = $actV;
        }
        return $actList;
    }
    /**
     * 计算审核结果
     * @param array $auditValue
     */
    function reckonAuditRs($auditValue){
        //审核状态标记
        $auditStatus = array();
        foreach ((array)$auditValue as $v){
            isset($auditStatus[(int)$v['auditRs']]) || $auditStatus[(int)$v['auditRs']] = 0;
            $auditStatus[(int)$v['auditRs']] ++;
        }
        $sumCount = array_sum($auditStatus);
        if($sumCount > 0){
            if ($auditStatus[0] == $sumCount){
                return 0;//未审核
            }elseif ($auditStatus[1] == $sumCount){
                return 1;//合格
            }elseif ($auditStatus[2] == $sumCount || $auditStatus[-1] == $sumCount){
                return 2;//不合格
            }else{
                //return 3;//部分合格
                return 2;//取消部分合格
            }
        }else{
            return 0;
        }
    }
    /**
     * 统计专营店审核结果数
     * @param number $proId
     */
    function countStoreAuditRs($proId){
        $auditCount = array(
            'auditing' => 0,//待审核
            'audited' => 0,//已审核
        );
        $list = $this->getList(array('storeId !='=>0,'proId'=>$proId,'auditRs >= '=>0), NULL, NULL, 'auditRs');
        foreach ($list as $v){
            if ($v['auditRs'] == 0){
                $auditCount['auditing'] ++;
            }else{
                $auditCount['audited'] ++;
            }
        }
        return $auditCount;
    }
}